package com.sky.canal;

import com.sky.constant.RedisConstant;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.utils.RedisCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: sky-take-out
 * @Author: XieMingHui
 * @CreateTime: 2023-11-27  15:08
 * @Description: 菜品分类canal监听器
 * @Version: 1.0
 */
@CanalTable("category")
@Component
@Slf4j
public class CategoryCanal implements EntryHandler<Category> {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private RedisCacheUtil redisCacheUtil;

    /**
     * 有新增
     *
     * @author: XieMingHui
     * @param: [category]
     * @return: void
     **/
    @Override
    public void insert(Category category) {
        Integer categoryType = category.getType();
        String key = RedisConstant.CATEGORY_TYPE_KEY + categoryType;
        //查询数据库
        List<Category> list = categoryMapper.list(categoryType);
        List<Category> listNull = categoryMapper.list(null);
        //更新redis缓存
        redisCacheUtil.setRedis(key, list, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        redisCacheUtil.setRedis(RedisConstant.CATEGORY_TYPE_KEY + "null", listNull, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        log.info("数据库中新增的数据:{}", category);


    }

    /**
     * 有更新
     *
     * @author: XieMingHui
     * @param: [category]
     * @return: void
     **/
    @Override
    public void update(Category before, Category after) {
        Integer categoryType = after.getType();
        String key = RedisConstant.CATEGORY_TYPE_KEY + categoryType;
        //查询数据库
        List<Category> list = categoryMapper.list(categoryType);
        List<Category> listNull = categoryMapper.list(null);
        //更新redis缓存
        redisCacheUtil.setRedis(key, list, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        redisCacheUtil.setRedis(RedisConstant.CATEGORY_TYPE_KEY + "null", listNull, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        log.info("数据库中更新的数据:{}更新后{}", before,after);
    }

    /**
     * 有删除
     *
     * @author: XieMingHui
     * @param: [category]
     * @return: void
     **/
    @Override
    public void delete(Category category) {
        Integer categoryType = category.getType();
        String key = RedisConstant.CATEGORY_TYPE_KEY + categoryType;
        //查询数据库
        List<Category> list = categoryMapper.list(categoryType);
        List<Category> listNull = categoryMapper.list(null);
        //更新redis缓存
        redisCacheUtil.setRedis(key, list, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        redisCacheUtil.setRedis(RedisConstant.CATEGORY_TYPE_KEY + "null", listNull, RedisConstant.LOGIC_TIME, TimeUnit.DAYS);
        log.info("数据库中删除的数据:{}", category);
    }
}
